﻿# -*- coding:utf-8 -*-

'''
Created on 2012-9-17

@author: jezhang
'''

from ebp.models import Config
from google.appengine.api import urlfetch
import Cookie
import HTMLParser
import datetime
import logging
import urllib



logging.info('module base reloaded')

def process_ebp_html(token): 
    dicts = Config.getEbpServerInfo()
    for item in dicts:        
        logging.info("%s=%s" %(item, dicts[item]))
        
    login_url = "%s" %dicts["login_url"]
    order_url = "%s" %dicts["order_url"]
    logging.info("Not found %s in OrderHdr table. Need to fetch data from %s" % (token, login_url))
    login_data = urllib.urlencode({
            "userID":("%s" %dicts["userID"]), 
            "passwd":("%s" %dicts["passwd"]), 
            "confirmword":""})
    logging.info(login_data)
    opener = URLOpener()
    response = opener.open(login_url, login_data)
    logging.info("Post() %s\nResponse code = %s" % (login_url, response.status_code))
    logging.info(opener.cookie)
    response = opener.open(order_url)
    logging.info("Get() %s\nResponse code = %s" % (order_url, response.status_code))
    ebp_orgi_html = response.content
    logging.info(ebp_orgi_html)
    ebp_html = ebp_orgi_html.decode('gbk').encode('utf-8')
    logging.info(ebp_html)
    start_index = ebp_html.index("<!--------------------------begin detail data table------------------------------------------->")
    end_index = ebp_html.index("<!--------------------------end detail data table------------------------------------------->")
    order_html = ebp_html[start_index:end_index]
    parser = EbpHtmlParser()
    parser.feed(order_html)
    return parser

def filterComma(vs):
    if vs.endswith(','):
        newstr = vs[:len(vs)-1]    
        return newstr
    else:
        return vs
      
def gbk2utf8(vs):
    return vs.decode('gbk').encode('utf-8')    

def to_utf8(vs):
    return vs.encode('utf-8')

def getChinaNow():    
    return datetime.datetime.utcnow() + datetime.timedelta(hours=+8)

def urldecode(value):
    return  urllib.unquote(urllib.unquote(value)).decode('utf8')
    #return  urllib.unquote(value).decode('utf8')

def urlencode(value):
    return urllib.quote(value.encode('utf8'))

def sid():
    now=getChinaNow()
    return now.strftime('%y%m%d%H%M%S')+str(now.microsecond)

def today():
#    now = datetime.datetime.date();
    return getChinaNow().strftime('%Y%m%d')
#    return time.strftime('%Y%m%d',time.localtime(time.time()))


class EbpHtmlParser(HTMLParser.HTMLParser):
    def __init__(self):
        HTMLParser.HTMLParser.__init__(self)
        self.prices = []
        self.names = []
        self.qtys = []
        self.count = 0
    
    def handle_starttag(self, tag, attrs):
        if tag == 'input':
            for name,value in attrs:                             
                if name == 'value' or name == 'name':
                    if value.find('price') > 0:                    
                        goods_price =  attrs[3][1]
                    #    print goods_price
                        self.prices.append(goods_price)
                        self.count += 1
                                                
                    if value.find('productDesc') > 0:
                        goods_name = attrs[3][1]
                    #    print goods_name
                        self.names.append(goods_name)
                        
                    if value.find('qty1') > 0:
                        goods_qty = attrs[2][1]
                    #    print goods_qty
                        self.qtys.append(goods_qty[:-2])  # 15.0 remove .0
                  
    def saveToDb(self, token):
         
        '''             
        for i in range(self.count):
            item = Goods()
            item.sn = (i + 1)
            item.token = token
            item.price = float(self.prices[i])
            item.name = unicode(self.names[i],"utf-8")
            item.qty_assigned = int(self.qtys[i])  
            item.qty_ordered = 0
            item.sum_ordered = 0.0
            item.createdate = getChinaNow()
            item.put()
            logging.info("Added new Goods(%s,%s,%s,%s)" %(item.sn,item.name,item.qty_assigned,item.price))
        '''        

    def deleteByToken(self, token):
        # delete the data which have exist already for token
        '''
        q = Goods.all()
        q.filter("token", token)
        for goods in q:
            goods.delete()
        '''      




class URLOpener():
    def __init__(self):
        self.cookie = Cookie.SimpleCookie()
        
    def open(self, url, data=None):
        if data is None:
            method = urlfetch.GET
        else:
            method = urlfetch.POST
            
        while url is not None:
            response = urlfetch.fetch(url=url, 
                                      payload=data, 
                                      method=method, 
                                      headers=self._getHeaders(self.cookie),
                                      allow_truncated=False,
                                      follow_redirects=False,
                                      deadline=10
                                      )
            data = None # Next request will be a get, so no need to send the data again. 
            method = urlfetch.GET
            self.cookie.load(response.headers.get('set-cookie', '')) # Load the cookies from the response
            url = response.headers.get('location')
        return response
    
    def _getHeaders(self, cookie):
        headers = {
                   'Host' : 'www.google.com',
                   'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)',
                   'Cookie' : self._makeCookieHeader(cookie)
                   }    
        return headers
    
    def _makeCookieHeader(self, cookie):        
        a=[""]
        for value in cookie.values():
            a.append("%s=%s; " % (value.key, filterComma(value.value)))
#            cookieHeader += "%s=%s; " % (value.key, filterComma(value.value))
        return ("".join(a))
  
